<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Class PartsScan
 */
class PartsScan extends MY_Controller{
    private $t_pParts = 'mt_product_parts';
    function __construct() {
        parent::__construct();
    }


    /**
     * 配件扫描
     */
    function index()
    {
        //获取表单数据
        $data = $formField = gp();
        //查询条件
        $where = array();
        $keyword = trim($formField['keyword']);
        $searchStr = 'products/PartsScan/?';
        if($keyword){
            if(substr($keyword,0,2)=='wx'){
                $where = array('order_num'=>$keyword);
            }else{
                $where = is_numeric($keyword)?array('pid'=>$keyword):array('sku'=>$keyword);
            }
        }
        //分页
        $this->load->library('pagination');
        $config['page_query_string'] = TRUE;
        $config['base_url'] = blang_url($searchStr);
        $config['total_rows'] = $this->common_model->countRows($this->t_pParts, $where);
        $config['per_page'] = 20;
        $page = max(1, intval($data['page']));
        $config['uri_segment'] = 5;
        $slimit = ($page - 1) * $config['per_page'];
        $this->pagination->initialize($config);
        $dataList = $this->common_model->select($this->t_pParts, $slimit, $config['per_page'], 'id asc', $where);
        $data['show_pages'] = $this->pagination->create_links();
        //重构数据
        $statusArr = array(1=>'Normal',2=>'pending',3=>'wait purchase');
        foreach ($dataList as $key => &$rs) {
            $rs['statusName'] = $statusArr[$rs['status']];
            $rs['add_time'] = date('Y-m-d',$rs['add_time']);
            $rs['nameSub'] = mb_substr($rs['name'], 0, 30);
            if(mb_strlen($rs['name'])>38){
                $rs['nameSub'] .= '...';
            }
            $rs['price'] = $rs['price'] . ' ' . $rs['currency'];
            $rs['rate_price'] = $rs['rate_price'] . ' ' . $rs['rate_currency'];
        }
        unset($rs);
        $data['dataList'] = $dataList;

        if($this->debug){
            gsh_output($data);
        }
        //视图以及样式
        $this->load->view('defaults/parts_scan',$data);
        $this->load->view('defaults/common/footer');
    }



    /**
     * 配件到货入库
     */
    function partsArrival()
    {
        //表单处理
        $data['post_type'] = __FUNCTION__;
        //接收数据
        $formData = gp();
        $ids = $formData['partsIds'];
        if ($ids) {
            foreach ($ids as $idVal) {
                $this->common_model->update($this->t_pParts,array('status'=>1),array('id'=>$idVal));
            }
        }
        //跳转
        redirect('products/PartsScan/');
    }
}